#define CORE_NONE 0
#define CORE_RUN 1
#define CORE_STOP 2
#define CORE_PAUSE 3

objectdef BotCore inherits BotBase
{
	variable int CurrentState = 0
	variable bool NeedQuit = FALSE
	variable bool NeedPause = TRUE
	variable int  PulsePriority = 50
	
	member State()
	{
		if ${This.NeedQuit}
		{
			return CORE_STOP
		}
		
		if ${This.NeedPause}
		{
			return CORE_PAUSE
		}

		return CORE_RUN
	}

	method Pulse()
	{
		This.CurrentState:Set[${This.State}]
		switch ${This.CurrentState}
		{
			case CORE_STOP
				This:EventQuit
				break
			case CORE_PAUSE
				break
			case CORE_RUN
				This:CorePulse
				break
		}
	}

	function Run()
	{
		do
		{
			while ${QueuedCommands}
			{
				ExecuteQueued
				FlushQueued
			}

			This:Pulse
			waitframe
		}
		while ${This.CurrentState} != CORE_STOP
	}

	method CorePulse()
	{
		Event[CORE_EVENT_PULSE]:Execute[${This.PulsePriority}]
	}

	method EventPause(bool Pause)
	{
		if ${Pause}
		{
			This:EA["Received pause signal"]
		}
		else
		{
			This:EA["Received resume signal"]
		}
		This.NeedPause:Set[${Pause}]
	}

	method EventQuit()
	{
		This:EA["Received quit signal"]
		This.NeedQuit:Set[TRUE]
	}
	 
	method Inject()
	{
		LavishScript:RegisterEvent[CORE_EVENT_PULSE]
		LavishScript:RegisterEvent[CORE_EVENT_PAUSE]
		LavishScript:RegisterEvent[CORE_EVENT_STOP]
		
		Event[CORE_EVENT_PAUSE]:AttachAtom[${This.ObjectName}:EventPause]
		Event[CORE_EVENT_STOP]:AttachAtom[${This.ObjectName}:EventQuit]
	}
	 
	method Shutdown()
	{
		Event[CORE_EVENT_PAUSE]:DetachAtom[${This.ObjectName}:EventPause]
		Event[CORE_EVENT_STOP]:DetachAtom[${This.ObjectName}:EventQuit]
		
		Event[CORE_EVENT_PULSE]:Unregister
		Event[CORE_EVENT_PAUSE]:Unregister
		Event[CORE_EVENT_STOP]:Unregister
	}

}
